package com.jumper.study.admin.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.admin.dto.req.LessonChapterReq;
import com.jumper.study.admin.dto.req.ModelStatusReq;
import com.jumper.study.admin.dto.resp.LessonChapterResp;
import com.jumper.study.admin.service.LessonChapterService;
import com.jumper.study.admin.service.LessonService;
import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.api.ResultCode;
import com.jumper.study.db.model.LessonChapter;
import com.jumper.study.db.model.Lesson;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;



@RestController
@RequestMapping("/api/backend/lessons/chapters")
public class LessonChapterController {
    @Autowired
    private LessonChapterService chapterService;

    @Autowired
    private LessonService lessonService;

    @GetMapping("/{id}")
    public CommonResult<LessonChapterResp> getItem(@PathVariable Long id) {
        LessonChapter chapter = chapterService.getById(id);
        if (chapter == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        Lesson lesson = lessonService.getById(chapter.getLessonId());
        if (lesson == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }

        LessonChapterResp resp = new LessonChapterResp(chapter.getId(), chapter.getTitle(), chapter.getLessonId(), lesson.getTitle(), 
            chapter.getVideo(), chapter.getDuration(), chapter.getOnself(),chapter.getSort(), chapter.getUpdatedAt());
        return CommonResult.success(resp);
    }
    
    @PostMapping("/")
    public CommonResult<Boolean> create(@RequestBody @Validated LessonChapterReq req) {
        Lesson lesson = lessonService.getById(req.getLessonId());
        if (lesson == null) {
            return CommonResult.failed(ResultCode.VALIDATE_FAILED.getCode(), "课程不存在");
        }
        LessonChapter chapter = new LessonChapter();
        chapter.setLessonId(req.getLessonId());
        chapter.setTitle(req.getTitle());
        chapter.setVideo(req.getVideo());
        chapter.setDuration(req.getDuration());
        chapter.setOnself(false);
        chapter.setSort(req.getSort());

        boolean result = chapterService.save(chapter);
        return CommonResult.success(result);
    }
    
    @PostMapping("/{id}")
    public CommonResult<Boolean> update(@PathVariable Long id, @RequestBody @Validated LessonChapterReq req) {
        LessonChapter chapter = chapterService.getById(id);
        if (chapter == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }

        chapter.setTitle(req.getTitle());
        chapter.setVideo(req.getVideo());
        chapter.setDuration(req.getDuration());
        chapter.setOnself(false);
        chapter.setSort(req.getSort());

        boolean result = chapterService.updateById(chapter);
        return CommonResult.success(result);
    }


    @DeleteMapping("/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        LessonChapter chapter = chapterService.getById(id);
        if (chapter == null) {
            return CommonResult.success(true);
        }

        boolean result = chapterService.removeById(id);
        return CommonResult.success(result);
    }

    @PostMapping("/onself")
    public CommonResult<Boolean> onself(@RequestBody @Validated ModelStatusReq req) {
        boolean result = chapterService.onselfBatchById(req.getIds(), req.status());

        return CommonResult.success(result);
    }
    
}
